{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#-*- coding: utf-8 -*-"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 1989, True, {'version': 3.7}]\n",
      "{'version': 3.7}\n"
     ]
    }
   ],
   "source": [
    "#2.1 列表  序列类型  数据有位置顺序 能够通过位置序号访问其中元素\n",
    "#列表是一个可以使用多种类型元素，支持元素都增，删，改，查操作都序列类型\n",
    "a = [\"Python\",1989,True,{\"version\":3.7}]\n",
    "print (a)\n",
    "print (a[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd', 'e', 'f', 'g']\n"
     ]
    }
   ],
   "source": [
    "#列表的另一种产生方式：list(可迭代对象)\n",
    "#可迭代对象包括：字符串、元组、集合、range()\n",
    "#字符串转列表\n",
    "print(list(\"abcdefg\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['we', 'are', 'one', 'family']\n"
     ]
    }
   ],
   "source": [
    "#元组转列表\n",
    "print (list((\"we\",\"are\",\"one\",\"family\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['lilei', 'Green', 'hanmeimei', 'jim']\n"
     ]
    }
   ],
   "source": [
    "#集合转列表\n",
    "print (list({\"lilei\",\"hanmeimei\",\"jim\",\"Green\"}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#特殊的range()\n",
    "for i in  [0,1,2,3,4,5]:\n",
    "    print (i)\n",
    "for i in range(6):\n",
    "    print (i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sdfasddfassdf99999999999999999999\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "sdfsdfsdfsddfsddfsddfsfsdfsdfsdf\n"
     ]
    }
   ],
   "source": [
    "#range(起始数字，中止数字，数字间隔)\n",
    "#如果起始数字缺省，默认为0，不包含中止数字，数字间隔缺省，默认为1\n",
    "print(\"sdfasddfassdf99999999999999999999\")\n",
    "for i in range(1,11):\n",
    "    print(i)\n",
    "print(\"sdfsdfsdfsddfsddfsddfsfsdfsdfsdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "#range转列表\n",
    "print(list(range(1,11,2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "#列表的性质\n",
    "#列表的长度  len(列表)\n",
    "ls = [1,2,3,4,5]\n",
    "print (len(ls))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BYD\n",
      "TOYOTA\n"
     ]
    }
   ],
   "source": [
    "#列表的索引，与同为序列类型的字符串完全相同\n",
    "#变量名[位置编号]，正向索引从0开始，方向索引从-1开始\n",
    "cars = [\"BYD\",\"BMW\",\"AUDI\",\"TOYOTA\"]\n",
    "print(cars[0])\n",
    "print (cars[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['BYD', 'BMW', 'AUDI']\n",
      "['BMW', 'TOYOTA']\n",
      "['BYD', 'BMW', 'AUDI', 'TOYOTA']\n",
      "['BYD', 'BMW']\n"
     ]
    }
   ],
   "source": [
    "#列表的切片\n",
    "#变量名[开始位置：结束位置：切片间隔]\n",
    "#正向切片\n",
    "print (cars[:3])\n",
    "print (cars[1:4:2])\n",
    "print (cars[:])\n",
    "print (cars[-4:-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['TOYOTA', 'AUDI', 'BMW']\n",
      "['TOYOTA', 'AUDI', 'BMW', 'BYD']\n"
     ]
    }
   ],
   "source": [
    "#反向切片\n",
    "print(cars[:-4:-1]) #开始位置缺省，默认为-1\n",
    "print(cars[::-1]) #获得反向列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "#列表的操作符\n",
    "#用list1+list2 的形式实现列表的拼接\n",
    "a = [1,2]\n",
    "b = [3,4]\n",
    "print (a+b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "#用n*list或者list*n的方式实现列表的成倍复制\n",
    "#列表初始化的一种方式\n",
    "print ([0]*10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Pyhton', 'C++', 'R', 'Java']\n"
     ]
    }
   ],
   "source": [
    "#列表的操作方法\n",
    "#增加元素  在末尾增加元素  列表.append(待增加元素)\n",
    "languages = [\"Pyhton\",\"C++\",\"R\"]\n",
    "languages.append(\"Java\")\n",
    "print (languages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Pyhton', 'C', 'C++', 'R', 'Java']\n"
     ]
    }
   ],
   "source": [
    "#在任意位置插入元素  列表.insert(位置编号，待增元素)，在位置编号相应元素前插入待增元素\n",
    "languages.insert(1,\"C\")\n",
    "print (languages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Pyhton', 'C', 'C++', 'R', 'Java', 'Ruby', 'PHP']\n"
     ]
    }
   ],
   "source": [
    "#在末尾整体插入另一列表\n",
    "# 列表1.extend(列表2)，extend将列表2的元素逐个添加到列表1中\n",
    "languages = ['Pyhton','C','C++','R','Java']\n",
    "languages.extend(['Ruby','PHP'])\n",
    "print (languages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Pyhton', 'C', 'C++', 'R', 'Java', 'Ruby', 'PHP', ['Ruby', 'PHP']]\n"
     ]
    }
   ],
   "source": [
    "# append将列表2整体作为一个元素添加到列表1中\n",
    "languages.append(['Ruby','PHP'])\n",
    "print (languages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C', 'R', 'Java']\n"
     ]
    }
   ],
   "source": [
    "#删除元素 删除列表i位置的元素 列表.pop(位置)\n",
    "languages =  [\"Python\",\"C++\",\"C\",\"R\",\"Java\"]\n",
    "languages.pop(1)\n",
    "print (languages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C', 'R']\n"
     ]
    }
   ],
   "source": [
    "#不写位置信息，默认删除最后一个元素\n",
    "languages.pop()\n",
    "print (languages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'R', 'Java']\n"
     ]
    }
   ],
   "source": [
    "#删除列表中的第一次出现的待删除元素， 列表.remove(待删元素)\n",
    "languages = ['Python','C','R','Java']\n",
    "languages.remove('C')\n",
    "print (languages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "#查找元素  列表中第一次出现待查找元素待位置  列表.index(待查元素)\n",
    "languages = ['Python','C','R','Java']\n",
    "idx = languages.index(\"Python\")\n",
    "print (idx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C++', 'R', 'Java']\n"
     ]
    }
   ],
   "source": [
    "#修改元素  通过\"先索引后赋值\"的方式，对元素进行修改 列表名[位置]=新值\n",
    "languages = ['Python','C','R','Java']\n",
    "languages[1] = \"C++\"\n",
    "print (languages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C', 'R']\n",
      "['Python', 'C', 'R', 'Java']\n"
     ]
    }
   ],
   "source": [
    "#列表复制\n",
    "#方法1： 列表.copy()\n",
    "languages = ['Python','C','R','Java']\n",
    "languages_2 = languages.copy()\n",
    "languages.pop()\n",
    "print (languages)\n",
    "print (languages_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C', 'R']\n",
      "['Python', 'C', 'R', 'Java']\n"
     ]
    }
   ],
   "source": [
    "#方法2：列表[:]\n",
    "languages = ['Python','C','R','Java']\n",
    "languages_3 = languages[:]\n",
    "languages.pop()\n",
    "print (languages)\n",
    "print (languages_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 2, 3, 5, 7, 8, 19]\n"
     ]
    }
   ],
   "source": [
    "#列表的排序\n",
    "#使用 列表.sort()对列表进行永久排序 直接在列表上进行操作，无返回值\n",
    "ls = [2,5,2,8,19,3,7]\n",
    "ls.sort()\n",
    "print (ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[19, 8, 7, 5, 3, 2, 2]\n"
     ]
    }
   ],
   "source": [
    "#递减排序\n",
    "ls1 = [2,5,2,8,19,3,7]\n",
    "ls1.sort(reverse=True)\n",
    "print (ls1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 2, 3, 5, 7, 8, 19]\n"
     ]
    }
   ],
   "source": [
    "#使用sorted(列表）对列表进行临时排序，原列表保持不变，返回排序后的列表\n",
    "ls2 = [2,5,2,8,19,3,7]\n",
    "ls_2 = sorted(ls2)\n",
    "print(ls_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 4, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "#列表的翻转 使用 列表.reverse() 对列表进行永久翻转 直接在列表上进行操作，无返回值\n",
    "ls3 = [1,2,3,4,5]\n",
    "ls3.reverse()\n",
    "print (ls3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#使用for循环对列表进行遍历\n",
    "ls4 = [1,2,3,4,5]\n",
    "for i in ls4:\n",
    "    print (i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "#元组 序列顺序 但是不支持修改，增加，减少 即不可变带列表\n",
    "#其他操作与列表操作完全一致\n",
    "b = (1,2,3,4,5)\n",
    "print (b[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(9, 27)\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "#打包与解包\n",
    "def f1(x):\n",
    "    return x**2,x**3  #实现打包返回，即将结果作为一个元组返回\n",
    "print (f1(3))\n",
    "print (type(f1(3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "27\n"
     ]
    }
   ],
   "source": [
    "#解包处理\n",
    "a,b = f1(3)\n",
    "print (a)\n",
    "print (b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(201901, 'xiaoming'), (201902, 'xiaohong'), (201903, 'xiaoqiang')]\n"
     ]
    }
   ],
   "source": [
    "#解包例子2\n",
    "numbers = [201901,201902,201903]\n",
    "name = [\"xiaoming\",\"xiaohong\",\"xiaoqiang\"]\n",
    "#将列表numbers和name用zip进行打包之后，就将各自相同位置的元素变成一个元组\n",
    "print(list(zip(numbers,name)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201901 xiaoming\n",
      "201902 xiaohong\n",
      "201903 xiaoqiang\n"
     ]
    }
   ],
   "source": [
    "#每次取到一个元素，立即进行解包赋值\n",
    "for number,name in zip(numbers,name):\n",
    "    print(number,name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明\n"
     ]
    }
   ],
   "source": [
    "#字典 映射类型，通过键-值带映射实现数据的存储和查找，内部是无序的，字典的键不能重复\n",
    "student = {201901:\"小明\",201902:\"小红\",201903:\"小强\"}\n",
    "#字典的索引 通过 字典[键]的形式来获取对应的值\n",
    "print (student[201901])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "student = {}\n",
    "#字典的键必须是不可变类型，如果键变了，就找不到对应的存储的值了\n",
    "#不可变类型： 数字、字符串、元组。一旦确定，它就是它自己，变了就不是它了，这些可以作为字典的键\n",
    "#可变类型：列表、字典、集合。 一旦确定，还可以随意增删改\n",
    "\n",
    "#字典的长度--键值对的个数\n",
    "student1 = {201901:\"小明\",201902:\"小红\",201903:\"小强\"}\n",
    "print (len(student1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{201901: '小明', 201902: '小红', 201903: '小强', 201904: '小雪'}\n"
     ]
    }
   ],
   "source": [
    "#字典的操作方法\n",
    "#增加键值对 通过变量名[新键] = 新值\n",
    "student2 = {201901:\"小明\",201902:\"小红\",201903:\"小强\"}\n",
    "student2[201904] = \"小雪\"\n",
    "print (student2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{201901: '小明', 201902: '小红'}\n"
     ]
    }
   ],
   "source": [
    "#删除键值对\n",
    "#通过del 变量名[待删除键] 删除键值对\n",
    "student3 = {201901:\"小明\",201902:\"小红\",201903:\"小强\"}\n",
    "del student3[201903]\n",
    "print (student3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小强\n",
      "{201901: '小明', 201902: '小红'}\n"
     ]
    }
   ],
   "source": [
    "#通过变量名.pop(待删除键) 删除，同时获得待删除键值对对值\n",
    "student4 = {201901:\"小明\",201902:\"小红\",201903:\"小强\"}\n",
    "value = student4.pop(201903)\n",
    "print (value)\n",
    "print (student4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201903 小强\n",
      "{201901: '小明', 201902: '小红'}\n"
     ]
    }
   ],
   "source": [
    "#变量名.popitem()随机删除一个键值对，并以元组返回删除对键值对\n",
    "student5 = {201901:\"小明\",201902:\"小红\",201903:\"小强\"}\n",
    "key,value = student5.popitem()\n",
    "print(key,value)\n",
    "print (student5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小雪\n"
     ]
    }
   ],
   "source": [
    "#修改键值对对值 通过先索引后赋值对方法对相应对值进行修改\n",
    "student6 = {201901:\"小明\",201902:\"小红\",201903:\"小强\"}\n",
    "student6[201902] = \"小雪\"\n",
    "print (student6[201902])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'牛': 2, '奶': 5, '找': 1, '刘': 1, '买': 1}\n"
     ]
    }
   ],
   "source": [
    "#d.get()方法 　d.get(key,default) 从字典d中获取键key对应对值，如果没有这个值，则返回default\n",
    "#统计 \"牛奶奶找刘奶奶买牛奶\"中字符出现对频率\n",
    "s = \"牛奶奶找刘奶奶买牛奶\"\n",
    "d = {}\n",
    "for i in s:\n",
    "    d[i] = d.get(i,0) + 1\n",
    "print (d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[201901, 201902, 201903]\n",
      "['xiaoming', 'xiaohong', 'xiaoqiang']\n",
      "[(201901, 'xiaoming'), (201902, 'xiaohong'), (201903, 'xiaoqiang')]\n"
     ]
    }
   ],
   "source": [
    "#d.keys() 取字典里所有的键\n",
    "#d.values()  取字典里所有的值\n",
    "student6 = {201901:\"xiaoming\",201902:\"xiaohong\",201903:\"xiaoqiang\"}\n",
    "print (list(student6.keys()))\n",
    "print (list(student6.values()))\n",
    "#d.items()方法及字典的遍历\n",
    "print(list(student6.items()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201901 xiaoming\n",
      "201902 xiaohong\n",
      "201903 xiaoqiang\n"
     ]
    }
   ],
   "source": [
    "for k,v in student6.items():\n",
    "    print (k,v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'xiaoqiang', 'xiaohong', 'xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "#集合 一系列互不相等元素的集合，无序且互斥\n",
    "#集合元素必须是不可变类型：数字，字符串或元组，可视作字典的键\n",
    "#集合可以看作是没有值，或者值为None的字典\n",
    "s = {\"xiaoming\",\"xiaohong\",\"xiaoqiang\",\"xiaoming\"} #可用于去重\n",
    "print (s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'zhong', 'zhang', 'lin', 'manyu', 'gu', 'liu'}\n"
     ]
    }
   ],
   "source": [
    "#集合的运算\n",
    "#例子 通过集合进行交集并集的运算\n",
    "Chinese_A = {\"liu\",\"zhang\",\"manyu\",\"zhong\",\"gu\",\"lin\"}\n",
    "print (Chinese_A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'guo', 'lin', 'manyu', 'li', 'liu', 'wang'}\n"
     ]
    }
   ],
   "source": [
    "Math_B = {\"lin\",\"guo\",\"wang\",\"liu\",\"manyu\",\"li\"}\n",
    "print (Math_B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'lin', 'manyu', 'liu'}\n"
     ]
    }
   ],
   "source": [
    "#交集运算，返回一个新集合，包括同时在A和B中的元素\n",
    "print  (Chinese_A&Math_B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'guo', 'zhong', 'zhang', 'lin', 'manyu', 'gu', 'li', 'liu', 'wang'}\n"
     ]
    }
   ],
   "source": [
    "#并集运算，返回一个包含A和B所有元素的新集合\n",
    "print (Chinese_A|Math_B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'li', 'guo', 'zhang', 'zhong', 'wang', 'gu'}\n"
     ]
    }
   ],
   "source": [
    "#返回包含A和B非共同元素的新集合\n",
    "print (Chinese_A^Math_B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'zhong', 'zhang', 'gu'}\n"
     ]
    }
   ],
   "source": [
    "#返回一个包含在集合A中但不在集合B中的元素的集合\n",
    "print (Chinese_A - Math_B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'zhang', 'manyu', 'wang', 'liu'}\n"
     ]
    }
   ],
   "source": [
    "#集合的操作方法\n",
    "#增加元素\n",
    "stars = {\"liu\",\"zhang\",\"manyu\"}\n",
    "stars.add(\"wang\")\n",
    "print (stars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "#移除元素\n",
    "stars.remove(\"wang\")\n",
    "#计算集合的长度\n",
    "print (len(stars))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhang\n",
      "manyu\n",
      "liu\n"
     ]
    }
   ],
   "source": [
    "#集合的遍历\n",
    "for star in stars:\n",
    "    print(star)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
